package com.aiyige.logic.user;

import android.text.TextUtils;
import android.util.Log;

import com.aiyige.BuildConfig;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.base.eventbus.EventLastPlayProgressUpdate;
import com.aiyige.model.TodoTasksModel;
import com.aiyige.model.request.GetUserTodoTasks;
import com.aiyige.model.request.UploadPlayProgressRequest;
import com.aiyige.page.login.model.impl.LoginManager;
import com.aiyige.utils.ToastX;

import org.greenrobot.eventbus.EventBus;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by xiachao on 2017/8/10.
 */

public class UserDataManager {
    public static final String TAG = UserDataManager.class.getSimpleName();
    private static UserDataManager mInstance;

    public static UserDataManager getInstance() {
        if (mInstance == null) {
            mInstance = new UserDataManager();
        }
        return mInstance;
    }

    public void logout() {
        releaseData();
    }

    private void releaseData() {

    }

    public void updatePlayProgress(String resourceId, int stage, long progress) {
        if (LoginManager.isLogin()) {
            //            播放集stage 服务器要求从1开始
            UploadPlayProgressRequest.newBuilder().resourceId(resourceId).stage(stage).progress(progress).build().enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    try {
                        if (response.isSuccessful()) {

                        }
                    } catch (Exception e) {

                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {

                }
            });
            EventBus.getDefault().post(EventLastPlayProgressUpdate.newBuilder().resourceId(resourceId).stage(stage).progress(progress).build());
            Log.i("xiachaoPlayerIndex",resourceId+":"+stage+":"+progress);
        }
    }

    /**
     * 根据用户id查询用户待做任务
     * @param type login-登录时检查,withdraw-提现时检查
     */
    public void requestUserTodoTasks(String type,final UserTodoCallback callback) {
        if (TextUtils.isEmpty(type)) {
            return;
        }
        if (LoginManager.isLogin()) {
            GetUserTodoTasks.newBuilder().location(type).build().enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    try {
                        if (response.isSuccessful()) {
                            String json = response.body().string();
                            if (BuildConfig.DEBUG) {
                                ToastX.show(json);
                            }
                            TodoTasksModel model = JsonUtil.toObject(json, TodoTasksModel.class);
                            if (model.getTaskName().equals(TodoTasksModel.TASK_COMMODITY_COOPERATION)) {

                            } else if (model.getTaskName().equals(TodoTasksModel.TASK_USER_CERTIFICATION)) {

                            } else {
                                if (callback != null) {
                                    callback.onNothingTask();
                                }
                            }
                        } else if (response.code() == 404){
                            if (callback != null) {
                                callback.onNothingTask();
                            }
                        }
                    } catch (Exception e) {
                        if (callback != null) {
                            callback.onNothingTask();
                        }
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    if (callback != null) {
                        callback.onNothingTask();
                    }
                }
            });
        }
    }

    public interface UserTodoCallback{
        void onTodoTask(String task);

        void onNothingTask();
    }


}
